package cn.itcast.order.service;

import cn.itcast.order.clients.UserClient;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserClient userClient;
    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        //todo 需要访问用户服务  获取用户数据  根据用户ID查询用户

        //后台发请求 httpClient  --RestTemplate  Restful  --->Spring提供的模板  RedisTemplate
//        String url = "http://user-service/user/" + order.getUserId();
//        ResponseEntity<User> responseEntity = restTemplate.getForEntity(url, User.class);
//        User user = responseEntity.getBody();
        User user = userClient.queryById(order.getUserId());
        order.setUser(user);
        // 4.返回
        return order;
    }
}
